home *** CD-ROM | disk | FTP | other *** search
/ Practical Algorithms for Image Analysis / Practical Algorithms for Image Analysis.iso / TARFILE.GZ / tarfile / ch_3.2 / lpfltr / smooth.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-09-11  |  2.1 KB  |  70 lines

  1. /* 
  2.  * smooth.c
  3.  * 
  4.  * Practical Algorithms for Image Analysis
  5.  * 
  6.  * Copyright (c) 1997, 1998, 1999 MLMSoftwareGroup, LLC
  7.  */
  8.  
  9. /* SMOOTH:      function performs 2-D smoothing by separable, 1-D averaging
  10.  *                usage: smooth (imgIO, fltr1D, nFltr)
  11.  */
  12.  
  13. #include <images.h>
  14. #include <tiffimage.h>
  15.  
  16. smooth (imgIO, nFltr)
  17.      Image *imgIO;              /* input/output image structure */
  18.      long nFltr;                /* number of filter coefficients */
  19. {
  20.   Image *imgT;                  /* intermediate image structure */
  21.   unsigned char **imgIOn,       /* input/output image */
  22.   **imgInt;                     /* intermediate image */
  23.   long width, height;           /* size of image */
  24.   long sum;                     /* sum of filter convolution at a pixel */
  25.   long midFltr;                 /* middle coefficient index of filter */
  26.   long xEnd, yEnd;              /* end coefficients of convolution */
  27.   long x, y, i;
  28.  
  29. /* intermediate image for result of row-wise smoothing */
  30.   imgIOn = ImageGetPtr (imgIO);
  31.   height = ImageGetHeight (imgIO);
  32.   width = ImageGetWidth (imgIO);
  33.   imgT = ImageAlloc (height, width, 8);
  34.   imgInt = ImageGetPtr (imgT);
  35.  
  36. /* perform row-wise smoothing */
  37.   midFltr = (nFltr - 1) / 2;
  38.   yEnd = height - midFltr;
  39.   xEnd = width - midFltr;
  40.   for (y = 0; y < height; y++) {
  41.     for (x = midFltr; x < xEnd; x++) {
  42.       sum = imgIOn[y][x];
  43.       for (i = 1; i <= midFltr; i++) {
  44.         sum += imgIOn[y][x - i] + imgIOn[y][x + i];
  45.       }
  46.       imgInt[y][x] = (unsigned char) (sum / nFltr);
  47.     }
  48.   }
  49.  
  50. /* perform column-wise convolution */
  51.   for (y = midFltr; y < yEnd; y++) {
  52.     for (x = midFltr; x < xEnd; x++) {
  53.       sum = imgInt[y][x];
  54.       for (i = 1; i <= midFltr; i++)
  55.         sum += imgInt[y - i][x] + imgInt[y + i][x];
  56.       imgIOn[y][x] = (unsigned char) (sum / nFltr);
  57.     }
  58.   }
  59.  
  60. /* set border regions to zero */
  61.   for (y = 0; y < height; y++)
  62.     for (x = 0; x < midFltr; x++)
  63.       imgIOn[y][x] = imgIOn[y][width - x - 1] = 0;
  64.   for (y = 0; y < midFltr; y++)
  65.     for (x = 0; x < width; x++)
  66.       imgIOn[y][x] = imgIOn[height - y - 1][x] = 0;
  67.  
  68.   return (0);
  69. }
  70.